<?php
	include_once("config.php");
?>
<p class=titulo align="center"><? echo $msg->get('XML_TITLE_GERA'); ?>
<p class=titulo align="center"><? echo $msg->get('XML_TITLE_MATRIZ'); ?></p>
<p class=titulo align="center"><? echo $msg->get('XML_GERA_MATRIZ_SUBTITLE_4'); ?></p>
<p>
<?
	require_once("verifica.php");
?>

<? if (strlen(@$arquivo)==0) { ?>

		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MSG01'); ?>
		<br>
		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_mul_xml.php" method="post">
		<input type="radio" name="arquivo" value="nao" unchecked><? echo $msg->get('XML_GERA_MSG02'); ?><br>
		<input type="radio" name="arquivo" value="sim" unchecked><? echo $msg->get('XML_GERA_MSG03'); ?><br>
        <input type="radio" name="arquivo" value="alt" unchecked><? echo $msg->get('XML_GERA_MSG12'); ?><br>
        <input type="radio" name="arquivo" value="del" unchecked><? echo $msg->get('XML_GERA_MSG13'); ?><p>
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_NEXT'); ?>>
		</form>

<?php } else if (@$arquivo == "alt") {
    $usr_file=$_SESSION['ANAWEB']->RetornaArquivoBiblioteca();
    if (file_exists($usr_file))
    {
        $obj = DOMDocument::load($usr_file);
        $filhos = $obj->getelementsbytagname("matrix");
		if (!@$alterar)
		{
			if ($filhos->length)
			{
			    echo $msg->get('Selecione_Exemplo_Alterado')."<br><br>";
                $j = 0;
                for ($i=0; $i < $filhos->length; $i++)
                {
                    $mult = $filhos->item($i)->getelementsbytagname("num_rows");
                    if ($mult->length > 1)
                    {
    				    $j++;
					    echo "<p><a href=\"index.php?inc=gera_prob_matriz_mul_xml.php&arquivo=alt&alterar=".($i)."\"><u>".$msg->get('Exemplo')." ". $j."</u></a></p>";
                    }
                }
			}
			else
			{
				echo "N&atilde;o constam exemplos desse m&oacute;dulo na sua biblioteca de exemplos.";
			}
		}
		else if (!@$Submit)
		{
			echo $msg->get('Alteracao_Exemplo').":<br><br>";
			$filho = $filhos->item($alterar);
			$desc = $filho->getelementsbytagname("description")->item(0)->nodeValue;
			$com = $filho->getelementsbytagname("comments")->item(0)->nodeValue;
			$ref = $filho->getelementsbytagname("reference")->item(0)->nodeValue;
            $num_rows_A = $filho->getelementsbytagname("num_rows")->item(0)->nodeValue;
            $num_rows_B = $filho->getelementsbytagname("num_rows")->item(1)->nodeValue;
            $num_cols_A = $filho->getelementsbytagname("num_cols")->item(0)->nodeValue;
            $num_cols_B = $filho->getelementsbytagname("num_cols")->item(1)->nodeValue;
            for ($i=0; $i<$num_rows_A;$i++)
            {
                $cols = $filho->getelementsbytagname("row")->item($i);
                for ($j=0; $j<$num_cols_A; $j++)
                    $A[$i][$j] = $cols->getelementsbytagname("x")->item($j)->nodeValue;
            }
            for ($i=0; $i<$num_rows_B;$i++)
            {
                $cols = $filho->getelementsbytagname("row")->item($i+$num_rows_A);
                for ($j=0; $j<$num_cols_B; $j++)
                    $B[$i][$j] = $cols->getelementsbytagname("x")->item($j)->nodeValue;
            }
?>
		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_mul_xml.php" method="post">
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG05'); ?> <? echo @$num_rows_A ?> x <? echo @$num_cols_A ?>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG07'); ?> <? echo @$num_rows_B ?> x <? echo @$num_cols_B ?>
		<br><br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG02'); ?>
		<br><br>
		<? for ($i=0; $i<$num_rows_A; $i++) {
			for ($j=0; $j<$num_cols_A; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$A[$i][$j] ?>" maxlength="10" size="5">
   		<?    } //end for j ?>
		<br>
		<? } //end for i ?>
		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG08'); ?> :
		<br><br>
		<? for ($i=0; $i<$num_rows_B; $i++) {	
			for ($j=0; $j<$num_cols_B; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="B[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$B[$i][$j] ?>" maxlength="10" size="5">
   		<?    } //end for j ?>
		<br>
		<? } //end for i ?>
		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG09'); ?>
		<b></b>
		<input type="text" size='66' maxlength='60' name="desc" value="<? echo @$desc ?>">
		<br><br>
		<p align="left"><? echo $msg->get('XML_GERA_MSG08'); ?><br>
		<TEXTAREA name="com" cols="50" rows="3"><? echo @$com ?></TEXTAREA>
		<p>
		<p align="left"><? echo $msg->get('XML_GERA_MSG09'); ?><br>
		<TEXTAREA name="ref" cols="50" rows="3"><? echo @$ref ?></TEXTAREA>
		<p>		
		<input type="hidden" name="arquivo" value="<? echo @$arquivo ?>">
	    <input type="hidden" name="file_read" value="<? echo @$file_read ?>">
		<input type="hidden" name="num_rows_A" value="<? echo @$num_rows_A ?>">
		<input type="hidden" name="num_cols_A" value="<? echo @$num_cols_A ?>">
		<input type="hidden" name="num_rows_B" value="<? echo @$num_rows_B ?>">
		<input type="hidden" name="num_cols_B" value="<? echo @$num_cols_B ?>">
        <input type="hidden" name="alterar" value="<?php echo $alterar ?>">
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_GERA_MSG010'); ?>>
		</form>
<?php
		}
		else
		{
            $filho = $filhos->item($alterar);
            $desc = $filho->getelementsbytagname("description")->item(0)->nodeValue = @$desc;
            $com = $filho->getelementsbytagname("comments")->item(0)->nodeValue = @$com;
            $ref = $filho->getelementsbytagname("reference")->item(0)->nodeValue = @$ref;
            for ($i=0; $i<$num_rows_A;$i++)
            {
                $cols = $filho->getelementsbytagname("row")->item($i);
                for ($j=0; $j<$num_cols_A; $j++)
                    $cols->getelementsbytagname("x")->item($j)->nodeValue=$A[$i][$j];
            }
            for ($i=0; $i<$num_rows_B;$i++)
            {
                $cols = $filho->getelementsbytagname("row")->item($i+$num_rows_A);
                for ($j=0; $j<$num_cols_B; $j++)
                    $cols->getelementsbytagname("x")->item($j)->nodeValue=$B[$i][$j];
            }
            $obj->save($usr_file);
            echo "Exemplo alterado com sucesso!";
		}
    }
    else
    {
        echo "Voc&ecirc; ainda n&atilde;o criou uma biblioteca de exemplos!";
    }
 } else if ($arquivo == "del")
 {
    $usr_file=$_SESSION['ANAWEB']->RetornaArquivoBiblioteca();
    if (file_exists($usr_file))
    {
        $obj = DOMDocument::load($usr_file);
        $filhos = $obj->getelementsbytagname("matrix");
        if (!@$remover)
        {
            if ($filhos->length)
            {
                echo $msg->get('Selecione_Exemplo_Removido')."<br><br>";
                $j = 0;
                for ($i=0; $i < $filhos->length; $i++)
                {
                    $mult = $filhos->item($i)->getelementsbytagname("num_rows");
                    if ($mult->length > 1)
                    {
                        $j++;
                        echo "<p><a href=\"index.php?inc=gera_prob_matriz_mul_xml.php&arquivo=del&remover=".($i)."\"><u>".$msg->get('Exemplo')." ". $j."</u></a></p>";
                    }
                }
            }
            else
            {
                echo "N&atilde;o constam exemplos desse m&oacute;dulo na sua biblioteca de exemplos.";
            }
        }
        elseif(!@$Submit)
        {
        }
    }
 } else if (($arquivo=='sim')&((strlen(@$text_file)==0)||(!file_exists(@$text_file)))&(@$file_read!='ok')) { ?>

		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MSG04'); ?>
		<br>
		<form enctype=multipart/form-data action="index.php?inc=gera_prob_matriz_mul_xml.php" method="post">
		<a href=javascript:winopen('ajuda/ajdados1.php?idioma=PT_BR.php')><? echo $msg->get('XML_GERA_MSG05'); ?></a><p>
		<input type="file" name="text_file"><p>
		<? if ((strlen($text_file)!=0)&(!file_exists($text_file))) { ?>
              <? echo $msg->get('XML_GERA_MSG06'); ?>
        <? } ?>
		<input type="hidden" name="arquivo" value="<? echo $arquivo ?>">
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_NEXT'); ?>>
		</form>

<? } else if  (((strlen(@$num_rows_A)==0)or(@$num_rows_A<1)or(@$num_cols_A<1)or(strlen(@$num_cols_A)==0)or(strlen(@$num_rows_B)==0)or(@$num_rows_B<1)or(@$num_cols_B<1)or(strlen(@$num_cols_B)==0)or(@$num_cols_A!=@$num_rows_B)or(@$num_cols_A>10)or(@$num_cols_B>10)or(@$num_rows_A>10)or(@$num_rows_B>10) )& ($arquivo=='nao'))  { ?>

		<form enctype=multipart/form-data action="index.php?inc=gera_prob_matriz_mul_xml.php" method="post" onSubmit="return tamanhomax(this)">
		<? if (@$num_cols_A != @$num_rows_B) { ?>
			<? echo $msg->get('XML_GERA_MATRIZ_MSG06'); ?>
		<? } ?>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG05'); ?>
		<b></b>
		<input type="text" size='2' maxlength='2' name="num_rows_A" value="<? echo @$num_rows_A ?>">x
		<input type="text" size='2' maxlength='2' name="num_cols_A" value="<? echo @$num_cols_A ?>">
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG07'); ?>
		<b></b>
		<input type="text" size='2' maxlength='2' name="num_rows_B" value="<? echo @$num_rows_B ?>">x
		<input type="text" size='2' maxlength='2' name="num_cols_B" value="<? echo @$num_cols_B ?>">
		<br><br>
		<input type="hidden" name="arquivo" value="<? echo $arquivo ?>">
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_NEXT'); ?>>
		</form>

<? } else if  (strlen(@$desc)==0 || !checaNumerico(@$A) || !checaNumerico(@$B)) { ?>

		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_mul_xml.php" method="post">

        <? if ((@$arquivo=='sim')&(@$file_read!='ok')) {
        $fp = fopen($text_file, "r");
		$vet_dados = file($text_file);
		$num_rows_A = trim($vet_dados[0]);
		$num_cols_A = trim($vet_dados[1]);
		$num_rows_B = trim($vet_dados[2]);
		$num_cols_B = trim($vet_dados[3]);
		$cont = 3;
		for ($i=0; $i<$num_rows_A; $i++){
		   for ($j=0; $j<$num_cols_A; $j++){
		       $cont = $cont + 1;
			   $A[$i][$j]=trim($vet_dados[$cont]);
			   }
		}
		for ($i=0; $i< $num_rows_B; $i++) {
		   for ($j=0; $j< $num_cols_B; $j++) {
			   $cont = $cont + 1;
			   $B[$i][$j]=trim($vet_dados[$cont]);
			   }
		}
		fclose($fp);
		$file_read = ok;
		}
		if (isset($A))
		{
			echo "<b><font color=\"#ff0000\">Os valores para as matrizes devem ser num&eacute;ricos</b>";
		}
		?>

		<? if ($num_cols_A != $num_rows_B) { ?>
			<? echo $msg->get('XML_GERA_MATRIZ_MSG06'); ?>
		<? } ?>

		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG05'); ?> <? echo @$num_rows_A ?> x <? echo @$num_cols_A ?>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG07'); ?> <? echo @$num_rows_B ?> x <? echo @$num_cols_B ?>
		<br><br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG02'); ?>
		<br><br>
		<? for ($i=0; $i<$num_rows_A; $i++) {	
			for ($j=0; $j<$num_cols_A; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$A[$i][$j] ?>" maxlength="10" size="5">
   		<?    } //end for j ?>
		<br>
		<? } //end for i ?>
		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG08'); ?> :
		<br><br>
		<? for ($i=0; $i<$num_rows_B; $i++) {	
			for ($j=0; $j<$num_cols_B; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="B[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$B[$i][$j] ?>" maxlength="10" size="5">
   		<?    } //end for j ?>
		<br>
		<? } //end for i ?>
		<br>
		<p align="left"><? echo $msg->get('XML_GERA_MATRIZ_MSG09'); ?>
		<b></b>
		<input type="text" size='66' maxlength='60' name="desc" value="<? echo @$desc ?>">
		<br><br>
		<p align="left"><? echo $msg->get('XML_GERA_MSG08'); ?><br>
		<TEXTAREA name="com" cols="50" rows="3"><? echo @$com ?></TEXTAREA>
		<p>
		<p align="left"><? echo $msg->get('XML_GERA_MSG09'); ?><br>
		<TEXTAREA name="ref" cols="50" rows="3"><? echo @$ref ?></TEXTAREA>
		<p>		
		<input type="hidden" name="arquivo" value="<? echo @$arquivo ?>">
	    <input type="hidden" name="file_read" value="<? echo @$file_read ?>">
		<input type="hidden" name="num_rows_A" value="<? echo @$num_rows_A ?>">
		<input type="hidden" name="num_cols_A" value="<? echo @$num_cols_A ?>">
		<input type="hidden" name="num_rows_B" value="<? echo @$num_rows_B ?>">
		<input type="hidden" name="num_cols_B" value="<? echo @$num_cols_B ?>">
		<input class="button" type="submit" name="Submit" value=<? echo $msg->get('XML_GERA_MSG010'); ?>>
		</form>

<? } else {

$id_demo = 0;
//$usr_file = $DOCUMENT_ROOT."/user/".$id_usuario."/biblioteca.xml";
$usr_file=$_SESSION['ANAWEB']->RetornaArquivoBiblioteca();
$dem_file = $DOCUMENT_ROOT."/user/".$id_demo."/biblioteca.xml";

		//Funcoes DOMXML Versao => 2.4.21
function xmlentities($xml)
{
	$entities = array ('<' => '&lt;','&' => '&amp;',);
	if (is_string($xml)) { $xml = strtr($xml, $entities);};
	return $xml;
}
function gera_bib($xml_file,$desc,$num_rows_A,$num_cols_A,$num_rows_B,$num_cols_B,$A,$B,$com,$ref)
{
		if (file_exists($xml_file)) {
            $objeto = new DOMDocument();
            $objeto->preserveWhiteSpace=false;
            $objeto->load($xml_file);
		}
		else {
            $objeto=new DOMDocument('1.0');
		}
        $objeto->formatOutput=true;
        $raiz = $objeto->getelementsbytagname("problems");
        if ($raiz->length == 0)
        {
            $raiz=$objeto->createElement("problems");
            $raiz=$objeto->appendChild($raiz);
        }
        $raiz=$objeto->getelementsbytagname("problems")->item(0);
		
		$filho = $objeto->getelementsbytagname("matrices");
		if ($filho->length == 0) {
			$filho = $objeto->createElement("matrices");
            $filho = $raiz->appendChild("matrices");
		}
		$filho = $objeto->getelementsbytagname("matrices")->item(0);

        $matrix=$objeto->createElement("matrix");
        $matrix=$filho->appendChild($matrix);
        $description = $objeto->createElement("description",xmlentities(utf8_encode($desc)));   
        $description = $matrix->appendChild($description);

		$rows_A = $objeto->createElement("num_rows",$num_rows_A);
        $rows_A = $matrix->appendChild($rows_A);
		$cols_A = $objeto->createElement("num_cols",$num_cols_A);
		$cols_A = $matrix->appendChild($cols_A);
		$rows_B = $objeto->createElement("num_rows",$num_rows_B);
		$rows_B = $matrix->appendChild($rows_B);
		$cols_B = $objeto->createElement("num_cols",$num_cols_B);
		$cols_B = $matrix->appendChild($cols_B);
		for($i = 0; $i < $num_rows_A; $i++)
		{ 	$row = $objeto->createElement("row");
            $row = $matrix->appendChild($row);
			for($j = 0; $j < $num_cols_A; $j++)
			{ 	$x = $objeto->createElement("x",$A[$i][$j]);
                $x = $row->appendChild($x);
			}
		}
		for($i = 0; $i < $num_rows_B; $i++)
		{ 	$row = $objeto->createElement("row");
            $row = $matrix->appendChild($row);
			for($j = 0; $j < $num_cols_B; $j++)
			{ 	$x = $objeto->createElement("x",$B[$i][$j]);
				$x = $row->appendChild($x);
			}
		}
		if (strlen($com)) {
			$comen = $objeto->createElement("comments",xmlentities(utf8_encode($com)));    
			$comen = $matrix->appendChild($comen);
		}
		if (strlen($ref)) {
			$refer = $objeto->createElement("reference",xmlentities(utf8_encode($ref)));
			$refer = $matrix->appendChild($refer);
		}
        $objeto->save($xml_file);
}
gera_bib($usr_file,$desc,$num_rows_A,$num_cols_A,$num_rows_B,$num_cols_B,$A,$B,$com,$ref);
//gera_bib($dem_file,$desc,$num_rows_A,$num_cols_A,$num_rows_B,$num_cols_B,$A,$B,$com,$ref);
        ?>
		<p align="center"><? echo $msg->get('XML_GERA_MSG011'); ?></p>
        <?


} //end else

?>

		<form name="back" method="post" action="index.php?inc=gera_prob_matriz_mul_xml.php">
		<input class="button" type="submit" name="Submit" value="<? echo $msg->get('XML_BEGIN'); ?>">
		</form>

</body>
</html>
